package com.example.controller;

import com.example.config.handler.login.ClientUserDetails;
import com.example.utils.JSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.api.*;

@RestController
@RequestMapping("/open-api")
public class TestDemoController {

    @GetMapping("/business-1")
    @PreAuthorize("hasAuthority('sys.mdluser.page')")
    public Result getA() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        System.out.println(authentication);
        System.out.println(authentication.getAuthorities());
        ClientUserDetails principal = (ClientUserDetails) authentication.getPrincipal();
        return ResultBuilder.aResult()
                .data(principal)
                .msg("${test.message.a:测试国际化消息 A}")
                .build();
    }


    @GetMapping("/business-2")
    public Result getC() {
        Authentication authentication = SecurityContextHolder
                .getContext()
                .getAuthentication();
        System.out.println("登录信息：" + JSON.stringify(authentication));
        return ResultBuilder.aResult()
                .code(Result.SUCCESS_CODE)
                .data("模拟访问成功的响应数据")
                .msg("匿名接口，所有人可公开访问")
                .build();
    }

}
